from models import EmployeeModel, LoginModel
from flask import session
from exts import db
from werkzeug.security import generate_password_hash, check_password_hash  # 用于密码的加密操作
from utils import Info


def register(name, username, password):
    # employee = EmployeeModel.query.filter_by(email=email).first()
    # if not employee:
    #     print("您的邮箱没有资格注册账号!")
    #     # return False, 403  # 服务器理解请求，但拒绝执行它
    #     return False, Info("您的邮箱没有资格注册账号!")
    # else:
    #     if employee.account:
    #         print("邮箱已被注册，注册失败!")
    #         # return False, 409  # 请求的资源已存在
    #         return False, Info("邮箱已被注册，注册失败!")
    #     hash_password = generate_password_hash(password)
    #     user = LoginModel(username=username, password=hash_password, employee=employee)
    #     db.session.add(user)
    #     db.session.commit()
    #     # return True, 201  # 成功，并创建新资源
    #     return True, Info("注册成功")
    hash_password = generate_password_hash(password)
    user = LoginModel(username=username, password=hash_password, name=name)
    db.session.add(user)
    db.session.commit()
    return True, Info("注册成功")


def login(username, password):
    # user = LoginModel.query.filter_by(username=username).first()
    # if not user:
    #     print("该用户未被注册！")
    #     return False, Info("该用户未被注册")
    # else:
    #     if check_password_hash(user.password, password):
    #         print("登录成功")
    #         session["user_id"] = user.id
    #         user_info = {
    #             "username": user.username,
    #             "name": user.employee.name,
    #             "email": user.employee.email,
    #             "role": user.employee.identity.description
    #         }
    #         print(user_info)
    #         return True, Info("登录成功", user_info)
    #     else:
    #         print("密码错误！请重新输入密码")
    #         return False, Info("密码错误，请重新输入密码")
    user = LoginModel.query.filter_by(username=username).first()
    if not user:
        print("该用户未被注册！")
        return False, Info("该用户未被注册")
    else:
        if check_password_hash(user.password, password):
            print("登录成功")
            session["user_id"] = user.id
            user_info = {
                "username": user.username,
                "name": user.name,
                "id": user.id
                # "email": user.employee.email,
                # "role": user.employee.identity.description
            }
            print(user_info)
            return True, Info("登录成功", user_info)
        else:
            print("密码错误！请重新输入密码")
            return False, Info("密码错误，请重新输入密码")


def edit_password(id, password, username=None):
    # employee = EmployeeModel.query.filter_by(email=email).first()
    # user = employee.account[0]
    # if not user:
    #     print("邮箱未被注册")
    #     # return False, 404
    #     return False, Info("邮箱未被注册")
    # else:
    #     if username:
    #         user.username = username
    #     user.password = generate_password_hash(password)
    #     db.session.commit()
    #     return True, Info("修改成功")
    user = LoginModel.query.get(id)
    # user = LoginModel.query.filter_by(username=username).first()
    if not user:
        print("邮箱未被注册")
        # return False, 404
        return False, Info("邮箱未被注册")
    else:
        user.password = generate_password_hash(password)
        db.session.commit()
        return True, Info("修改成功")


